package com.ruicar.afs.cloud.basic.common.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.basic.common.condition.BasicInsuranceInfoCondition;
import com.ruicar.afs.cloud.basic.common.condition.ContractBasicListVO;
import com.ruicar.afs.cloud.basic.common.condition.QueryContractListCondition;
import com.ruicar.afs.cloud.basic.common.condition.QueryCustInfoMultipleCondition;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.mapper.BasicMainInfoMapper;
import com.ruicar.afs.cloud.basic.common.service.BasicContractCustRelationService;
import com.ruicar.afs.cloud.basic.common.service.BasicCustBaseInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.contractcancel.condition.BasicCancelInfoCondition;
import com.ruicar.afs.cloud.basic.contractcancel.vo.BasicCancelInfoVo;
import com.ruicar.afs.cloud.basic.costexempt.condition.BasicCostExemptCondition;
import com.ruicar.afs.cloud.basic.costexempt.vo.BasicExpenseRecordDetailVo;
import com.ruicar.afs.cloud.basic.costexemptbill.condition.BasicCostExemptBillCondition;
import com.ruicar.afs.cloud.basic.costexemptbill.vo.BasicCostExemptBillInfoVo;
import com.ruicar.afs.cloud.basic.detail.vo.QueryListVO;
import com.ruicar.afs.cloud.basic.proceeds.mq.config.ArchiveConfig;
import com.ruicar.afs.cloud.basic.proceeds.mq.sender.CommissionSender;
import com.ruicar.afs.cloud.basic.proceeds.mq.service.ArchiveMQService;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lee
 * @date 2020-05-30 04:35:07
 * @description
 */
@Service
@AllArgsConstructor
public class BasicMainInfoServiceImpl extends ServiceImpl<BasicMainInfoMapper, BasicMainInfo> implements BasicMainInfoService {

    private final BasicContractCustRelationService basicContractCustRelationService;
    private final BasicCustBaseInfoService basicCustBaseInfoService;
    private final CommissionSender commissionSender;
    private final ArchiveMQService archiveMQService;

    //多条件查询已激活案件合同
    @Override
    public List<ContractBasicListVO> findActivatedContractList(QueryContractListCondition condition) {
        return baseMapper.findActivatedContractList(condition);
    }

    //多条件查询已激活合同(获取条数)
    @Override
    public Integer findActivatedContractCount(QueryContractListCondition condition) {
        return baseMapper.findActivatedContractCount(condition);
    }

    @Override
    public IPage<List<BasicCancelInfoVo>> getBasicCancelList(Page page, BasicCancelInfoCondition condition) {
        return baseMapper.getBasicCancelList(page, condition);
    }

    @Override
    public IPage<List<BasicCancelInfoVo>> getBasicCancelLoanList(Page page, BasicCancelInfoCondition condition) {
        return baseMapper.getBasicCancelLoanList(page, condition);
    }

    @Override
    public IPage<List<QueryListVO>> queryContractInfo(Page page, QueryListVO query) {
        return baseMapper.queryContractInfo(page, query);
    }
    @Override
    public IPage<List<BasicCostExemptBillInfoVo>> getBasicCostBillList(Page page, BasicCostExemptBillCondition condition) {
        return baseMapper.getBasicCostBillList(page,condition);
    }

    @Override
    public IPage<List<BasicCostExemptBillInfoVo>> getBasicCostLoanList(Page page, BasicCostExemptBillCondition condition) {
        return baseMapper.getBasicCostBillLoanList(page,condition);
    }

    @Override
    public IPage<List<ContractBasicListVO>> getCanCostExemptListByCondition(Page page, BasicCostExemptCondition condition) {
        return baseMapper.getCanCostExemptListByCondition(page,condition);
    }

    @Override
    public IPage<BasicExpenseRecordDetailVo> getCanCostExemptListByContractNo(Page page, BasicCostExemptCondition condition) {
        return baseMapper.getCanCostExemptListByContractNo(page,condition);
    }

    @Override
    public List queryContractCount(BasicInsuranceInfoCondition condition) {
        //根据条件查询申请数量
        List custInfos = new ArrayList();
        QueryCustInfoMultipleCondition queryCondition = new QueryCustInfoMultipleCondition();
        queryCondition.setCustRole(condition.getCustRole());
        if(EmptyUtils.isNotEmpty(condition.getCertNo())){
            queryCondition.setCertNo(condition.getCertNo());
            custInfos = basicCustBaseInfoService.findByCustInfo(queryCondition);
        }else if(EmptyUtils.isNotEmpty(condition.getPhone())){
            queryCondition.setCertNo(condition.getPhone());
            custInfos = basicCustBaseInfoService.findByCustInfo(queryCondition);
        }else if(EmptyUtils.isNotEmpty(condition.getContractNo())){
            queryCondition.setContractNo(condition.getContractNo());
            custInfos = basicCustBaseInfoService.findByCustInfo(queryCondition);
        }
            return custInfos;
    }

    /**
     * 定时发送提前还款数据
     */
    @Override
    public void sendRepayMent() {
        List<BasicMainInfo> basicMainInfoList = this.list(Wrappers.<BasicMainInfo>query().lambda()
                .eq(BasicMainInfo::getContractStatus,ContractStatusEnum.advanceSettle)//合同状态为提前结清
                .eq(BasicMainInfo::getSettleDate,DateUtil.today())//合同结清日期等于当前时间
                .le(BasicMainInfo::getStartDate,DateUtil.today())//合同激活时间小于等于当前时间
        );
        for(BasicMainInfo basicMainInfo:basicMainInfoList){
            try {
                JSONObject json = new JSONObject();
                json.put("CamainNo",basicMainInfo.getApplyNo());//申请单号
                json.put("ContractNo",basicMainInfo.getContractNo());//合同号
                commissionSender.sendRepayMent(json.toJSONString());
            } catch (Exception e) {
                log.error("发送CMS合同提前还款数据数据异常[{0}],异常信息:[{1}]", e);
            }
        }
    }

    /**
     * 定时发送合同取消数据
     */
    @Override
    public void sendContractCancel() {
        List<BasicMainInfo> basicMainInfoList = this.list(Wrappers.<BasicMainInfo>query().lambda()
                .eq(BasicMainInfo::getContractStatus,ContractStatusEnum.contractCancel)//合同状态为取消
                .eq(BasicMainInfo::getCancelDate,DateUtil.today())//合同取消时间等于当天时间
                .le(BasicMainInfo::getStartDate,DateUtil.today())//合同激活时间小于等于当前时间
        );
        for(BasicMainInfo basicMainInfo:basicMainInfoList){
            try {
                JSONObject json = new JSONObject();
                json.put("CamainNo",basicMainInfo.getApplyNo());//申请单号
                json.put("ContractNo",basicMainInfo.getContractNo());//合同号
                commissionSender.sendContractCancel(json.toJSONString());
            } catch (Exception e) {
                log.error("发送CMS合同取消数据数据异常[{0}],异常信息:[{1}]", e);
            }
            try {
                archiveMQService.archivePushMQ(basicMainInfo.getId().toString(),basicMainInfo.getContractNo(),basicMainInfo.getApplyNo(),ArchiveConfig.wwcontract_flatcancel);
            } catch (Exception e) {
                log.error("发送归档系统合同取消数据数据异常[{0}],异常信息:[{1}]", e);
            }
        }
    }

}
